/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.facets;

import ru.perm.kefir.bbcode.BBProcessorFactory;
import ru.perm.kefir.bbcode.TextProcessor;
import unc.pds.auth.IPDSSecurity;
import unc.pds.auth.SecurityBRemote;
import unc.pds.model.DiaryRemote;
import unc.pds.model.Postable;
import unc.pds.model.UserRemote;

/**
 *
 * @author Михаил
 */
public class PostsFacet implements Facet {
    public PostsFacet(Postable user, IPDSSecurity security) {
        this.user = user;
        this.security = security;
    }
    private Postable user;
    private IPDSSecurity security;
    public String draw() throws Exception {
        String result = "";
        DiaryRemote[] diaries = user.getDiares();
        TextProcessor processor = BBProcessorFactory.getInstance().create();
        for (int i = 0; i < diaries.length; i++) {
            if (security.getPermission(diaries[i].getKey(), "read")) {
                result += "<h4>"+diaries[i].getCaption()+"</h4>"
                        + "<p><i>"+DateFormatter.format(diaries[i].getDate())+"</i></p>\n";
                result += "<p id=\"post\">"+processor.process(diaries[i].getDiary())+"</p>\n";
                result += "<p><a href=\"index.jsp?id="+diaries[i].getKey()+"\">На страницу записи</a></p>\n";
            }
        }
        return result;
    }
    public boolean isSide() {
        return false;
    }
}
